<?php

namespace backend\controllers;


/**
 * Created by PhpStorm.
 * User:ly
 * Date: 2017\11\20 0020
 * Time: 11:00
 *
 * 我的项目
 */
use backend\controllers\BaseController;
use backend\models\User;
use common\models\Country;
use common\models\Package;
use common\models\PackageInitiation;
use common\models\Project;
use common\models\ProjectTeam;
use Yii;
use common\models\ProjectLike;
use common\models\ProjectHighlights;
use yii\data\Pagination;
use common\models\UserModel;

class MyProjectController extends BaseController
{
    /**
     * 项目配置团队
     * project_team
     *  `manager` varchar(255) NOT NULL COMMENT '项目经理',
     * `owner` varchar(255) NOT NULL COMMENT '项目负责人',
     * `partner` varchar(255) NOT NULL COMMENT '项目协助人员',
     */
    public function actionMyInvolvement()
    {
        $use_id = Yii::$app->user->id;// Yii::$app->user->id
        $like_str = ',' . $use_id . ',';
        $projectState = array();
        //$data = ProjectTeam::find()->with('project.package','project.provinceName','project.cityName')->where(['manager' => $use_id])->orWhere(['owner' => $use_id])->orWhere(['like', 'partner', $like_str]);
        $data=ProjectTeam::find()->from(ProjectTeam::tableName().' as PT')
            ->select('P.code,P.borrower,C.title as provinceName,CC.title as cityName,P.principal,P.type2,PA.title,U.realname,PT.partner,P.guarantor,P.status,P.highlights,P.id,P.package_id,P.is_highlights')
            ->leftJoin(Project::tableName().' as P','P.id=PT.project_id')
            ->leftJoin(Package::tableName().' as PA','PA.id=P.package_id')
            ->leftJoin(Country::tableName().' as C','C.id=P.province')
            ->leftJoin(Country::tableName().' as CC','CC.id=P.city')
            ->leftJoin(User::tableName().' as U','U.id=PT.owner')
            ->where(['PT.manager' => $use_id])->orWhere(['PT.owner' => $use_id])->orWhere(['like', 'PT.partner', $like_str])->andWhere(['=','P.is_finished',1]);;
        //添加条件 andFilterWhere
        $getData=Yii::$app->request->get();
        if (Yii::$app->request->get('keyWords')|| Yii::$app->request->get('status')) {
            $keyWords=Yii::$app->request->get('keyWords');
            $status=Yii::$app->request->get('status');
            if(!empty($keyWords)){
                $data->andWhere("PA.title like :keyWords or P.borrower like :keyWords  or U.realname like :keyWords", [
                    ':keyWords' => '%'.$keyWords.'%'
                ]);
            }
            $data->andFilterWhere(['P.status'=>$status]);
        /*添加条件的 结束*/
        }
        //排序
        if(isset($getData['sort']) && $getData['sort']!=''){
            $frist = substr($getData['sort'], 0, 1 );
            $sortName=str_replace('-','',$getData['sort']);
            switch ($sortName)
            {
                case 'title'://资产包名称
                    $sortName='PA.'.$sortName;
                    break;
                case 'realname'://项目经理
                    $sortName='U.'.$sortName;
                    break;
                default:
                    $sortName='P.'.$sortName;
            }
            if($frist=='-'){//降序
                $data->orderBy($sortName.' DESC');
            }else{//升
                $data->orderBy($sortName.' ASC');
            }
        }else{//默认排序
            $data->orderBy('P.is_highlights ASC,P.id DESC');
        }
        $total = $data->count();
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 10]);    //$pageSize实例化分页类,带上参数(总条数,每页显示条数)
        $data_array = $data->offset($pages->offset)->limit($pages->limit)->asArray()->all();
        $hlCount = ProjectHighlights::find()->where('user_id=:user_id', [':user_id' => $use_id])->count();//亮点项目的个数
        $likeCount = ProjectLike::find()->where('user_id=:user_id', [':user_id' => $use_id])->count();//关注的项目个数
        //获取项目总监manager  项目经理owner  团队成员partner 联想数据
        $user_array=UserModel::find()->select('realname')->all();
        return $this->render('index', [
            'pages' => $pages,
            'data' => $data_array,
            'total' => $total,
            'hlCount' => $hlCount,
            'likeCount' => $likeCount,
            'projectState' => $projectState,
            'users'=>$user_array
        ]);
    }

    /**
     *我标记亮点的项目
     */
    public function actionMyHighLights()
    {
        $use_id = Yii::$app->user->id;
        $like_str = ',' . $use_id . ',';
        $projectState = array();
        //添加条件
        //$data = ProjectHighlights::find()->with('project.package','team','project.provinceName','project.cityName')->where('user_id=:user_id', [':user_id' => $use_id]);
        $data=ProjectLike::find()->from(ProjectLike::tableName().' as PL')
            ->select('P.code,P.borrower,C.title as provinceName,CC.title as cityName,P.principal,P.type2,PA.title,U.realname,PT.partner,P.guarantor,P.status,P.highlights,P.id,P.package_id,P.is_highlights')
            ->leftJoin(Project::tableName().' as P','P.id=PL.project_id')
            ->leftJoin(Package::tableName().' as PA','PA.id=P.package_id')
            ->leftJoin(Country::tableName().' as C','C.id=P.province')
            ->leftJoin(Country::tableName().' as CC','CC.id=P.city')
            ->leftJoin(ProjectTeam::tableName().' as PT','PT.project_id=PL.project_id')
            ->leftJoin(User::tableName().' as U','U.id=PT.owner')
            ->where(['PL.type'=>2])
            ->andWhere(['PL.user_id' => $use_id])->andWhere(['=','P.is_finished',1]);;
        //添加条件 andFilterWhere
        $getData=Yii::$app->request->get();
        if (Yii::$app->request->get('keyWords')|| Yii::$app->request->get('status')) {
            $keyWords=Yii::$app->request->get('keyWords');
            $status=Yii::$app->request->get('status');
            if(!empty($keyWords)){
                $data->andWhere("PA.title like :keyWords or P.borrower like :keyWords or U.realname like :keyWords", [
                    ':keyWords' => '%'.$keyWords.'%'
                ]);
            }
            $data->andFilterWhere(['P.status'=>$status]);
            /*添加条件的 结束*/
        }
        //排序
        if(isset($getData['sort']) && $getData['sort']!=''){
            $frist = substr($getData['sort'], 0, 1 );
            $sortName=str_replace('-','',$getData['sort']);
            switch ($sortName)
            {
                case 'title'://资产包名称
                    $sortName='PA.'.$sortName;
                    break;
                case 'realname'://项目经理
                    $sortName='U.'.$sortName;
                    break;
                default:
                    $sortName='P.'.$sortName;
            }
            if($frist=='-'){//降序
                $data->orderBy($sortName.' DESC');
            }else{//升
                $data->orderBy($sortName.' ASC');
            }
        }else{//默认排序
            $data->orderBy('P.is_highlights ASC,P.id DESC');
        }
        $total=$data->count();
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 10]);    //$pageSize实例化分页类,带上参数(总条数,每页显示条数)
        $data_array = $data->offset($pages->offset)->limit($pages->limit)->asArray()->all();
        $InCount = ProjectTeam::find()->where(['manager' => $use_id])->orWhere(['owner' => $use_id])->orWhere(['like', 'partner', $like_str])->count();//我参与的项目
        $likeCount = ProjectLike::find()->where('user_id=:user_id', [':user_id' => $use_id])->count();//关注的项目个数
        //获取项目总监manager  项目经理owner  团队成员partner 联想数据
        $user_array=UserModel::find()->select('realname')->all();
        return $this->render('show-high-lights', [
            'pages' => $pages,
            'data' => $data_array,
            'total' => $total,
            'InCount' => $InCount,
            'likeCount' => $likeCount,
            'projectState' => $projectState,
            'users'=>$user_array
        ]);
    }

    /**
     *我关注的项目
     */
    public function actionMyLike()
    {
        $use_id = Yii::$app->user->id;
        $like_str = ',' . $use_id . ',';
        $projectState = array();
        //$data = ProjectLike::find()->with('project.package','team','project.provinceName','project.cityName')->where('user_id=:user_id', [':user_id' => $use_id]);
        $data=ProjectLike::find()->from(ProjectLike::tableName().' as PL')
            ->select('P.code,P.borrower,C.title as provinceName,CC.title as cityName,P.principal,P.type2,PA.title,U.realname,PT.partner,P.guarantor,P.status,P.highlights,P.id,P.package_id,P.is_highlights')
            ->leftJoin(Project::tableName().' as P','P.id=PL.project_id')
            ->leftJoin(Package::tableName().' as PA','PA.id=P.package_id')
            ->leftJoin(Country::tableName().' as C','C.id=P.province')
            ->leftJoin(Country::tableName().' as CC','CC.id=P.city')
            ->leftJoin(ProjectTeam::tableName().' as PT','PT.project_id=PL.project_id')
            ->leftJoin(User::tableName().' as U','U.id=PT.owner')
            ->where(['PL.type'=>1])
            ->andWhere(['PL.user_id' => $use_id])->andWhere(['=','P.is_finished',1]);;
        //添加条件 andFilterWhere
        $getData=Yii::$app->request->get();
        if (Yii::$app->request->get('keyWords')|| Yii::$app->request->get('status')) {
            $keyWords=Yii::$app->request->get('keyWords');
            $status=Yii::$app->request->get('status');
            if(!empty($keyWords)){
                $data->andWhere("PA.title like :keyWords or P.borrower like :keyWords or U.realname like :keyWords", [
                    ':keyWords' => '%'.$keyWords.'%'
                ]);
            }
            $data->andFilterWhere(['P.status'=>$status]);
            /*添加条件的 结束*/
        }
        //排序
        if(isset($getData['sort']) && $getData['sort']!=''){
            $frist = substr($getData['sort'], 0, 1 );
            $sortName=str_replace('-','',$getData['sort']);
            switch ($sortName)
            {
                case 'title'://资产包名称
                    $sortName='PA.'.$sortName;
                    break;
                case 'realname'://项目经理
                    $sortName='U.'.$sortName;
                    break;
                default:
                    $sortName='P.'.$sortName;
            }
            if($frist=='-'){//降序
                $data->orderBy($sortName.' DESC');
            }else{//升
                $data->orderBy($sortName.' ASC');
            }
        }else{//默认排序
            $data->orderBy('P.is_highlights ASC,P.id DESC');
        }
        $hlCount = ProjectHighlights::find()->where('user_id=:user_id', [':user_id' => $use_id])->count();//亮点项目的个数
        $InCount = ProjectTeam::find()->where(['manager' => $use_id])->orWhere(['owner' => $use_id])->orWhere(['like', 'partner', $like_str])->count();//我参与的项目
        $total=$data->count();
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => 10]);    //$pageSize实例化分页类,带上参数(总条数,每页显示条数)
        $data_array = $data->offset($pages->offset)->limit($pages->limit)->asArray()->all();
        //获取项目总监manager  项目经理owner  团队成员partner 联想数据
        $user_array=UserModel::find()->select('realname')->all();
        return $this->render('show-my-like', [
            'pages' => $pages,
            'data' => $data_array,
             'total' => $total,
            'InCount' => $InCount,
            'hlCount' => $hlCount,
            'projectState' => $projectState,
            'users'=>$user_array
        ]);
    }
}